﻿namespace Shanda.DevFx.Security.Web.Pages
{
    using Shanda.DevFx.Security;
    using Shanda.DevFx.Security.Web;
    using Shanda.DevFx.Utils;
    using System;
    using System.Text;
    using System.Web.UI.WebControls;

    public class LeftPage : Shanda.DevFx.Security.Web.BasePage
    {
        protected Label lblMenu;
        protected Label lblUserName;

        protected virtual string BuildMenu(Permission[] ps)
        {
            int i = 0;
            return string.Format("<ul style=\"margin-left:30px\">{0}</ul>", this.BuildMenu0(ps, ref i));
        }

        protected string BuildMenu0(Permission[] ps, ref int i)
        {
            if (i >= ps.Length)
            {
                return string.Empty;
            }
            int layerIndex = ps[i].LayerIndex;
            StringBuilder builder = new StringBuilder();
            while (i < ps.Length)
            {
                Permission permission = ps[i];
                if (permission.LayerIndex == layerIndex)
                {
                    string argValue = permission.Res1;
                    string str2 = null;
                    if (!Checker.CheckEmptyString("URL", argValue, false))
                    {
                        argValue = base.ResolveUrl(argValue);
                    }
                    else
                    {
                        str2 = " onclick=\"return false;\"";
                        argValue = "#";
                    }
                    builder.AppendFormat("<li><a href=\"{1}\"{2}>{0}</a></li>", permission.Title, argValue, str2);
                    i++;
                }
                else
                {
                    if (permission.LayerIndex <= layerIndex)
                    {
                        break;
                    }
                    builder.AppendFormat("<ul style=\"margin-left:15px\">{0}</ul>", this.BuildMenu0(ps, ref i));
                    continue;
                }
            }
            return builder.ToString();
        }

        protected string BuildMenu1(Permission[] ps)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < ps.Length; i++)
            {
                string argValue = ps[i].Res1;
                if (Checker.CheckEmptyString("URL", argValue, false))
                {
                    builder.AppendFormat("　{0}<font face=\"Wingdings\">1</font> {1}<br />\r\n", new string('　', ps[i].LayerIndex), ps[i].Title);
                }
                else
                {
                    builder.AppendFormat("　{0}<font face=\"Wingdings\">F</font> <a href=\"{1}\">{2}</a><br />\r\n", new string('　', ps[i].LayerIndex), base.ResolveUrl(argValue), ps[i].Title);
                }
            }
            return builder.ToString();
        }

        protected override void InitData()
        {
            base.InitData();
            this.lblUserName.set_Text(string.Format("&nbsp;&nbsp;<b>你好：{0}</b>", base.get_User().get_Identity().get_Name()));
            Permission[] ps = Securitor.Authentication.CurrentRelations.SelectByPermissionViewState(true).DistinctByPermission(true).SortByPermissionDispIndex(false).ToPermissionArray();
            this.lblMenu.set_Text(this.BuildMenu1(ps));
        }

        private void InitializeComponent()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }
    }
}

